/*
 *  Copyright (c) 2001 Sun Microsystems, Inc.  All rights
 *  reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *  1. Redistributions of source code must retain the above copyright
 *  notice, this list of conditions and the following disclaimer.
 *
 *  2. Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in
 *  the documentation and/or other materials provided with the
 *  distribution.
 *
 *  3. The end-user documentation included with the redistribution,
 *  if any, must include the following acknowledgment:
 *  "This product includes software developed by the
 *  Sun Microsystems, Inc. for Project JXTA."
 *  Alternately, this acknowledgment may appear in the software itself,
 *  if and wherever such third-party acknowledgments normally appear.
 *
 *  4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"
 *  must not be used to endorse or promote products derived from this
 *  software without prior written permission. For written
 *  permission, please contact Project JXTA at http://www.jxta.org.
 *
 *  5. Products derived from this software may not be called "JXTA",
 *  nor may "JXTA" appear in their name, without prior written
 *  permission of Sun.
 *
 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *  DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 *  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 *  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 *  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 *  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 *  SUCH DAMAGE.
 *  ====================================================================
 *
 *  This software consists of voluntary contributions made by many
 *  individuals on behalf of Project JXTA.  For more
 *  information on Project JXTA, please see
 *  <http://www.jxta.org/>.
 *
 *  This license is based on the BSD license adopted by the Apache Foundation.
 *
 *  $Id: NetBeamsAddSensorPanel.java,v 1.2 2006/07/13 05:26:37 nano Exp $
 */
package net.jxta.myjxta.misc.beam;

import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;

/**
 * @author Taiwei.lin@sun.com
 */
public class NetBeamsAddSensorPanel extends JPanel {

    private String inputURL = null;
    private String inputXPath = null;
    private String inputType = null;
    private static JFrame frame = null;
    private JButton b1 = null;
    private JButton b2 = null;
    private JLabel l1 = null;
    private JLabel l2 = null;
    private JLabel l3 = null;
    private JTextField t1 = null;
    private JTextField t2 = null;
    private JComboBox t3 = null;
    public void setURL(String newURL) {
        this.inputURL = newURL;
    }

    public void setXPath(String newXPath) {
        this.inputXPath = newXPath;
    }

    public void setType(String newType) {
        this.inputType = newType;
    }

    public String getURL() {
        return this.inputURL;
    }

    public String getXPath() {
        return this.inputXPath;
    }

    public String getType() {
        return this.inputType;
    }

    /**
     * Creates a new instance of NetBeamsAddSensorPanel
     */
    public NetBeamsAddSensorPanel(String[] types) {

        setLayout(null);
        setBorder(new TitledBorder(new EtchedBorder(), "Add Sensor"));
        b1 = new JButton("Add");
        b1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b1ActionPerformed(evt);
            }
        });
        b2 = new JButton("Cancel");
        b2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b2ActionPerformed(evt);
            }
        });
        l1 = new JLabel();
        l1.setText("URL:");
        l1.setToolTipText("Enter the URL to a sensor data feed");
        l2 = new JLabel();
        l2.setText("XPath:");
        l2.setToolTipText("Enter the XPath expression for a data value in the feed");
        l3 = new JLabel();
        l3.setText("Type:");
        l3.setToolTipText("Choose or enter the data type for the data value in the feed");
        t1 = new JTextField();
        t2 = new JTextField();
        t3 = new JComboBox();
        t3.setEditable(true);
        if (types != null && types.length > 0) {
            for (int i = 0; i < types.length; i++) {
                t3.addItem(types[i]);
            }
        }

        add(b1);
        add(b2);
        add(l1);
        add(l2);
        add(l3);
        add(t1);
        add(t2);
        add(t3);

        Insets insets = this.getInsets();
        Dimension size = b1.getPreferredSize();
        b1.setBounds(120 + insets.left, 120 + insets.top, size.width, size.height);
        size = b2.getPreferredSize();
        b2.setBounds(240 + insets.left, 120 + insets.top, size.width, size.height);
        size = l1.getPreferredSize();
        l1.setBounds(20 + insets.left, 20 + insets.top, size.width, size.height);
        size = l2.getPreferredSize();
        l2.setBounds(20 + insets.left, 50 + insets.top, size.width, size.height);
        size = l3.getPreferredSize();
        l3.setBounds(20 + insets.left, 80 + insets.top, size.width, size.height);
        t1.setBounds(80 + insets.left, 20 + insets.top, 320, 20);
        t2.setBounds(80 + insets.left, 50 + insets.top, 320, 20);
        t3.setBounds(80 + insets.left, 80 + insets.top, 320, 20);
    }

    private void b2ActionPerformed(java.awt.event.ActionEvent evt) {
        frame.setVisible(false);
        frame.dispose();
    }

    private void b1ActionPerformed(java.awt.event.ActionEvent evt) {
        setURL(t1.getText());
        setXPath(t2.getText());
        setType(t3.getSelectedItem().toString());
        System.out.println("URL: " + t1.getText());
        System.out.println("XPath: " + t2.getText());
        System.out.println("Type: " + t3.getSelectedItem().toString());
        //todo: need to hook the user intput back to the beam panel
        frame.setVisible(false);
        frame.dispose();
    }

    private static void createAndShowGUI() {
        //Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated(true);

        //Create and set up the window.
        frame = new JFrame("NetBEAMS");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Set up the content pane.
        NetBeamsAddSensorPanel nb = new NetBeamsAddSensorPanel(null);
        nb.setPreferredSize(new Dimension(400, 300));
        frame.getContentPane().add(nb);

        //Size and display the window.
        Insets insets = frame.getInsets();
        frame.setSize(450 + insets.left + insets.right,
                220 + insets.top + insets.bottom);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
